package znbkTestPaperXtfx.saveXtfxPaper;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Handler;
import android.util.Log;

import com.example.lancooznbk.view.base.ILoadPaperView;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;

import znbkListXtfx.ISystemReviewPresenter;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperRdqh.activity.WeekAscensionPaperActivity;
import znbkTestPaperXtfx.ZnbkPaperXtfxInfor;
import znbkTestPaperXtfx.activity.SystemReviewPaperActivity;

/**
 * Created by Administrator on 2018\4\3 0003.
 */

public class SaveSystemReviewPaperPresenter  implements ISystemReviewPresenter {

    private ILoadPaperView view;
    private int ModelFrom;
    private boolean isFromExit = false;
    private boolean isAssess = false;

    public SaveSystemReviewPaperPresenter(ILoadPaperView view, int modelFrom) {
        this.view = (ILoadPaperView) view;
        this.ModelFrom = modelFrom;
    }

    @Override
    public void getData(boolean isShowLoading) {
        //  if (isShowLoading) view.showLoading();
        HttpOnNextListener<SubmitAnswerEntityResultXtfx> httpOnNextListener = new HttpOnNextListener<SubmitAnswerEntityResultXtfx>() {

            @Override
            public void onNext(SubmitAnswerEntityResultXtfx submitAnswerEntityResultXtfx) {

                if(submitAnswerEntityResultXtfx.isResult()){

                    if (isFromExit){
//                        ToastUtils.showShort("提交成功");
                        Log.e("==isFromExit==","======"+isFromExit);
                        view.getLoadingDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                                if(isAssess){
                                    view.openAssessDialog();
                                }else {
                                    view.openExamReportDialog();
                                }
                            }
                        });
                        view.updatingResult(true);

                    }else{
//                        ToastUtils.showShort("保存成功");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                ((Activity)view).finish();
                            }
                        },200);

                    }
                }else{
                    view.updatingResult(false);
                    ToastUtils.showShort("提交失败! 错误码"+submitAnswerEntityResultXtfx.isResult());
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                //view.showNetError();
                ToastUtils.showShort("提交失败Error!");
            }
        };

        SaveSystemReviewPaperApi api = new SaveSystemReviewPaperApi(httpOnNextListener, (SystemReviewPaperActivity)view);
        api.setmSaveSystemReviewPaperEntity(SaveSystemReviewPaperEntityModel.getSaveSystemReviewPaperEntity(ZnbkPaperXtfxInfor.AnsweredItemCount,
                    ZnbkPaperXtfxInfor.AnswerState,ZnbkPaperXtfxInfor.Duration,ZnbkPaperXtfxInfor.PaperId,ZnbkPaperXtfxInfor.PaperName,
                    UserInfo.StuId,UserInfo.SubjectId, UserInfo.SchoolId,ZnbkPaperXtfxInfor.ModelItemAnswerDetailListBean));

        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void getMoreData() {
        getData(false);
    }

    @Override
    public void getDataByStatus(int position) {
        getData(false);
    }

    public void getData(boolean isShowing ,boolean isFromExit,boolean isAssess){
        this.isFromExit = isFromExit;
        this.isAssess = isAssess;
        getData(isShowing);
    }

}